var $;
layui
    .config({
        base: '../layui/exts/', //静态资源所在路径
    })
    .use(['table', 'form', 'util'], function () {
        lui = layui;
        var form = layui.form;
        $ = layui.$;
        let util = layui.util;
        let pid, tid, classifyId, fillingModel;
        form.render();

        pid = $("#projectList").val();

        //创建表格
        let table = layui.table;
        openTaskData(table, pid);

        form.on("select(projectList)", function (data) {
            pid = data.value;
            openTaskData(table, pid);
        });

        //监听工具条
        table.on("row(taskTable)", function (obj) {
            var data = obj.data;
            obj.tr.addClass("layui-table-click").siblings().removeClass("layui-table-click");
            tid = data.id;
            classifyId = data.archivesClassifyId;
            fillingModel = data.fillingModel;
            getFields();
        });

        let cols = []

        function getFields() {
            cols = [];
            let col = []
            //案卷表格初始化
            $.ajax({
                type: 'POST',
                url: '/basic/getShowFieldList',
                dataType: "JSON",
                sync: false,
                data: {"classifyId": classifyId, "fillingModel": fillingModel},
                success: function (res) {
                    col.push({type: 'checkbox', fixed: 'left'});
                    $.each(res.data, function (index, obj) {
                        let leftFixed = "";
                        if (obj.fixedLeft === "1") {
                            leftFixed = "left";
                        }
                        col.push({fixed: leftFixed, field: obj.fieldName, width: obj.showWidth, title: obj.fieldTitle});
                    });
                    col.push({fixed: 'right', title: '操作', width: 60, toolbar: '#filesOperationBar'});
                    cols.push(col);
                    openFileData(cols);
                }
            });
        }


        function openFileData(cols, value) {
            let data = [];
            data["pid"] = pid;
            data["tid"] = tid;
            data["archiveCode"] = "";
            if (value !== undefined) {
                data["archiveCode"] = value;
            }
            let div = document.getElementById('fileData');
            table.render({
                elem: '#fileDataTable',
                url: '../archives/getUserReceiveData',
                contentType: "application/json",
                method: 'POST',
                cellMinWidth: 50,
                page: true,
                limit: 50,
                height: "full-112",
                loading: true,
                size: 'sm',
                where: data,
                cols: cols,
                done: function (res, curr, count) {
                    eData = res.data;
                    if (count === 0) {
                        fileId = "/";
                    }
                    //表格加载完成后，默认选中第一行
                    $('.layui-table-view[lay-table-id="fileDataTable"]').children(".layui-table-box").children(".layui-table-body").find('table tbody tr[data-index="0"]').click();
                },
            });
        }

        //监听选择行
        table.on("row(fileDataTable)", function (obj) {
            var data = obj.data;
            obj.tr.addClass("layui-table-click").siblings().removeClass("layui-table-click");
            fileId = data.id;
        });

        form.on('submit(fileSearch)', function (data) {
            openFileData(cols, data.field.archiveCode);
            return false;
        });


        //监听工具条
        table.on('tool(fileDataTable)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case "finish2":
                    top.layer.confirm("确定所选择的案卷完成该工序了吗？", function (index) {
                        $.ajax({
                            url: './finishWorkPiece', //url
                            contentType: "application/json",
                            dataType: 'json', //数据格式
                            type: 'POST', //请求方式
                            async: false, //是否异步请求
                            data: JSON.stringify({
                                pid: pid,
                                tid: tid,
                                archivesId: data.id,
                                postReceive: 1,
                                receiveId: data.receiveId,
                                num: 1,
                                workNum: workNum,
                                fillingModel: fillingModel
                            }),
                            success: function (result) {
                                if (result.code == 0) {
                                    table.reload('fileDataTable');
                                    top.layer.close(index);
                                } else {
                                    top.parent.layer.alert(result.message, {icon: 2});
                                }
                            },
                        });
                        return false;
                    });
                    break;
            }
        });


        //监听工具条
        table.on('tool(recordDataTable)', function (obj) {
            var data = obj.data;
        });

        util.on("lay-action", {
            batchFinish2: function () {
                var data = table.checkStatus('fileDataTable').data;
                if (data.length < 1) {
                    top.parent.layer.alert("没有选择已完成工序的档案", {icon: 0});
                    return false;
                }
                var ids = [];
                for (let i = 0; i < data.length; i++) {
                    ids.push(data[i].id);
                }
                top.layer.confirm("确定所选择的案卷已完成工序处理了吗？", function (index) {
                    $.ajax({
                        url: './batchFinishWorkPiece', //url
                        contentType: "application/json",
                        dataType: 'json', //数据格式
                        type: 'POST', //请求方式
                        async: false, //是否异步请求
                        data: JSON.stringify({
                            pid: pid,
                            tid: tid,
                            ids: ids,
                            workNum: workNum,
                            fillingModel: fillingModel
                        }),
                        success: function (result) {
                            if (result.code === 0) {
                                table.reload('fileDataTable');
                                top.layer.close(index);
                                top.parent.layer.alert(result.message, {icon: 1});
                            } else {
                                top.parent.layer.alert(result.message, {icon: 2});
                            }
                        },
                    });
                    return false;
                });
            }
        });
    });


